package com.dream.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.dream.able.MessageBoardMapper;
import com.dream.datasource.DBContextHolder;
import com.dream.pojo.MessageBoard;
import com.dream.pojo.MessageBoardExample;
import com.dream.pojo.MessageBoardSon;
import com.dream.service.able.All_Service_able;

/*
 * 留言的业务类
 */
@Service("Message_Board_Serviceimpl")
public class Message_Board_Serviceimpl 
implements All_Service_able<MessageBoardSon>{
	
	/*
	 * 留言表的DAO代理对象
	 */
	@Resource(name="messageBoardMapper")
	private MessageBoardMapper mapper;

	/*
	 * (添加一条留言,传入，内容，用户id)
	 * @see com.dream.service.able.All_Service_able#insert(java.lang.String[])
	 */
	@Override
	public MessageBoardSon insert(String... str) {
		//切换数据库，主库
		DBContextHolder.setDbType(DBContextHolder.master);
		//创建一条评论对象
		MessageBoardSon board = new MessageBoardSon();
		//插入内容
		board.setContext(str[0]);
		//插入时间
		board.setTime(new Date());
		//插入用户id
		board.setUid(Integer.parseInt(str[1]));
		//插入状态
		board.setState(1);
		//写入数据库
		return mapper.insertSelective(board)>0?board:null;
	}

	/*
	 * (查询所有评论)
	 * @see com.dream.service.able.All_Service_able#list()
	 */
	@Override
	public List<MessageBoardSon> list() {
		//切换数据库，从库
		DBContextHolder.setDbType(DBContextHolder.slave);
		//创建查询工具类
		MessageBoardExample boardExample = new MessageBoardExample();
		//设置排序规则
		boardExample.setOrderByClause("message_boardmid desc");
		//创建查询条件工具内部类
		MessageBoardExample.Criteria criteria = boardExample.createCriteria();
		//设置查询条件,state=1
		criteria.andStateEqualTo(1);
		//返回查询到的结果集
		return mapper.listMessageBoard(boardExample);
	}

	@Override
	public MessageBoardSon getT(String... str) {
		// TODO Auto-generated method stub
		return null;
	}

}
